package com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzxczh.constants.FxfzConstants;
import com.css.fxfzxczh.constants.YNEnum;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.entity.EarEveManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.earEveManage.service.EarEveManageService;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.entity.HeaManage;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.queryParams.HeaManageQuaParams;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.repository.HeaManageRepository;
import com.css.fxfzxczh.modules.synthesizeInfoManage.heaManage.service.HeaManageService;
import com.css.fxfzxczh.util.CreateGson;
import com.css.fxfzxczh.util.PlatformObjectUtils;
import com.css.fxfzxczh.util.UUIDGenerator;
import com.css.fxfzxczh.web.PlatformSessionContext;
import com.css.fxfzxczh.zcpt.sys.entity.SUser;
import com.css.fxfzxczh.zcpt.sys.service.SUserService;
import com.css.fxfzxczh.zcpt.sys.service.SysRoleService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HeaManageServiceImpl implements HeaManageService {

    @Resource
    HeaManageRepository heaManageRepository;

    @Resource
    SysRoleService sysRoleService;

    @Resource
    EarEveManageService earEveManageService;

    @Resource
    SUserService sUserService;


    @Override
    public Map<String, Object> getPage(int curPage, int pageSize,String xsemId) {
        Map<String,Object> page=heaManageRepository.getPage(curPage, pageSize,xsemId);
        return page;
    }

    @Override
    public void delete(String id) {
        HeaManage heaManage = heaManageRepository.findById(id);
        String userId = PlatformSessionContext.getUserID();
        String role = sysRoleService.getRoleByUserId(userId);
        StringBuilder str = new StringBuilder("");
        List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
        for (Map roleMap : roleMaps) {
            str.append(roleMap.get("roleCode") + ",");
        }
        String roles = str.toString();
        if (roles.contains(FxfzConstants.XCZH_COUNTRY_ROLE)) {
            heaManageRepository.delete(id);
        }
        if (roles.contains(FxfzConstants.XCZH_PROVINCE_ROLE)) {
            if (!userId.equals(heaManage.getCreateUser())) {
                throw new RuntimeException("删除失败，只能删除自己本省下的数据");
            }
            heaManageRepository.delete(id);
        }
    }

    @Override
    @Transactional
    public void save(HeaManage heaManage) {
        heaManage.setXhmId(UUIDGenerator.getUUID());
        heaManage.setDelFlag(YNEnum.N.toString());
        heaManageRepository.save(heaManage);
        earEveManageService.updateRelationStatus(heaManage.getXsemId());
    }

    @Override
    public void update(HeaManage heaManage) {
        String userId = PlatformSessionContext.getUserID();
        String role = sysRoleService.getRoleByUserId(userId);
        StringBuilder str=new StringBuilder("");
        List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
        for(Map roleMap:roleMaps){
            str.append(roleMap.get("roleCode")+",");
        }
        String roles = str.toString();
        heaManage.setUpdateTime(new Date());
        heaManage.setUpdateUser(userId);
        if (roles.contains(FxfzConstants.XCZH_COUNTRY_ROLE)) {
            heaManageRepository.update(heaManage);
        }
        if(roles.contains(FxfzConstants.XCZH_PROVINCE_ROLE)){
            if (!userId.equals(heaManage.getCreateUser())) {
                throw new RuntimeException("修改失败，只能修改自己本省下的数据");
            }
            heaManageRepository.update(heaManage);
        }
    }

    @Override
    public Map<String,Object> getHeadInfo(String eventCode) {
        EarEveManage currentEvent = earEveManageService.getCurrentEvent();
        eventCode=currentEvent.getEventCode();
        Map<String,Object> map=new HashMap<>();

        StringBuilder headCode = new StringBuilder(eventCode+"ZHB");
        DecimalFormat decimalFormat=new DecimalFormat("00");
        String num="";

        String maxNum=heaManageRepository.getMaxNum(headCode.toString());
        if(PlatformObjectUtils.isEmpty(maxNum)){
            int i = 1;
            num=decimalFormat.format(i);
        }else{
            String substring = maxNum.substring(maxNum.length() - 2);
            num = decimalFormat.format(Integer.parseInt(substring) + 1);
        }
        map.put("headCode",headCode.append(num).toString());

        String userId = PlatformSessionContext.getUserID();
        String user = sUserService.getSUser(userId);
        SUser sUser = CreateGson.createGson().fromJson(user, SUser.class);
        map.put("createUserName", sUser.getUserName());
        map.put("createUser", sUser.getUserId());
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        map.put("createTime", dateFormat.format(new Date()));
        //map.put("createUnit", sUser.getDeptEntity().getDeptName());
        map.put("createUnit", sUser.getOrgName());
        return map;

    }

    @Override
    public HeaManage findById(String id) {
        return heaManageRepository.findById(id);
    }

    /*@Override
    public String getHeadCode(String eventCode) {
        StringBuilder str = new StringBuilder(eventCode+"ZHB");
        DecimalFormat decimalFormat=new DecimalFormat("00");
        String num="";

        String maxNum=heaManageRepository.getMaxNum(str.toString());
        if(PlatformObjectUtils.isEmpty(maxNum)){
            int i = 1;
            num=decimalFormat.format(i);
        }else{
            String substring = maxNum.substring(15);
            num = decimalFormat.format(Integer.parseInt(substring) + 1);
        }
        return str.append(num).toString();
    }*/
}
